<?php

class Comment_article_model extends CI_Model {

    public function __construct() {
        parent::__construct();
        $this->load->database();
    }

    private $tbl = 'comment_article';

    function addComment($id_ar, $id_u, $content) {
        $this->load->helper('date');

        $data = array(
            'id_article' => $id_ar,
            'id_user' => $id_u,
            'content' => $content,
            'date' => standard_date('DATE_ATOM', time())
        );
        $this->db->insert($this->tbl, $data);
        return $this->db->insert_id();
    }

    function loadByArticle($id_ar, $limit = NULL, $offset = NULL) {
        $this->db->select();
        $this->db->from($this->tbl);
        $this->db->join('users', $this->tbl . '.id_user = users.id_user');
        $this->db->where('id_article', $id_ar);
        $this->db->limit($limit, $offset);
        $this->db->order_by('id_cmt_article', 'desc');
        return $this->db->get();
    }

    function loadById($id) {
        $this->db->select();
        $this->db->from($this->tbl);
        $this->db->join('users', $this->tbl . '.id_user = users.id_user');
        $this->db->where('id_cmt_article', $id);
        return $this->db->get();
    }

    function countAll() {
        return $this->db->count_all($this->tbl);
    }

}
